package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //如果购物车中已经存在该商品，则数量加一
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCartDTO);
        if (list != null && list.size() > 0){
            ShoppingCart shoppingCart = list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.update(shoppingCart);
        }
        //否则添加到购物车
        else {
            ShoppingCart shoppingCart = null;
            //判断本次添加的是菜品，还是套餐
            if (shoppingCartDTO.getDishId() != null){
                //是菜品
                DishVO dish = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart = ShoppingCart.builder()
                        .dishId(shoppingCartDTO.getDishId())
                        .name(dish.getName())
                        .image(dish.getImage())
                        .amount(dish.getPrice())
                        .dishFlavor(shoppingCartDTO.getDishFlavor())
                        .build();
            } else {
                //是套餐
                SetmealVO setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart = ShoppingCart.builder()
                        .setmealId(shoppingCartDTO.getSetmealId())
                        .name(setmeal.getName())
                        .image(setmeal.getImage())
                        .amount(setmeal.getPrice())
                        .build();
            }
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartMapper.save(shoppingCart);
        }
    }

    /**
     * 购物车列表
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        List<ShoppingCart> list = shoppingCartMapper.showShoppingCart(BaseContext.getCurrentId());
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    /**
     * 购物车减一
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //根据菜品id查询购物车
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCartDTO);
        //如果购物车中存在该商品，则数量减一
        if(list != null && list.size() > 0) {
            //查看要减少的是菜品还是套餐
            if (shoppingCartDTO.getDishId() != null) {
                //是菜品
                //获取购物车中的菜品数量
                ShoppingCart shoppingCart = list.get(0);
                int number = shoppingCart.getNumber();
                //如果数量大于1，则数量减一
                if (number > 1) {
                    shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                    shoppingCartMapper.update(shoppingCart);
                }
                else {
                    //如果数量等于1，则删除该商品
                    shoppingCartMapper.delete(shoppingCart);
                }

            } else {
                //是套餐
                //获取购物车中的套餐数量
                ShoppingCart shoppingCart = list.get(0);
                int number = shoppingCart.getNumber();
                //如果数量大于1，则数量减一
                if (number > 1) {
                    shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                    shoppingCartMapper.update(shoppingCart);
                }
                else {
                    //如果数量等于1，则删除该商品
                    shoppingCartMapper.delete(shoppingCart);
                }
            }
        }
        else {
            //如果购物车中不存在该商品，则提示用户
            log.info("购物车中不存在该商品");
            throw new DeletionNotAllowedException(MessageConstant.UNKNOWN_ERROR);
        }
    }
}
